package com.yhg.xinyi.movie;


import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.yhg.xinyi.R;
import com.yhg.xinyi.bean.movieInfoBean;

import java.util.List;

public class MovieInfoAdapter extends BaseAdapter {
    Context context;
    List<movieInfoBean> mDatas;
    ImageLoader imageLoader;
    DisplayImageOptions options;   // 图片加载配置信息

    public MovieInfoAdapter(Context context, List<movieInfoBean> mDatas) {
        this.context = context;
        this.mDatas = mDatas;
        imageLoader = ImageLoader.getInstance();
        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(null)
                .showImageForEmptyUri(null)
                .showImageOnFail(null)
                .cacheInMemory(true).cacheOnDisk(true).considerExifParams(true)
                .bitmapConfig(Bitmap.Config.RGB_565).build();
    }

    @Override
    public int getCount() {
        return mDatas.size();
    }

    @Override
    public Object getItem(int position) {
        return mDatas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
//        1.判断内存中是否有复用的view
        if (convertView == null) {
//            2.将布局转换成新的view进行使用
            convertView = LayoutInflater.from(context).inflate(R.layout.item_movie_lv,null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }
//        获取指定位置的数据源
        movieInfoBean dataBean = mDatas.get(position);
        holder.nameTv.setText(dataBean.getCname());
        holder.infoTv.setText(dataBean.getInfo());
        holder.scoreTv.setText(dataBean.getScore());
        holder.ratedTv.setText(dataBean.getRated());
        holder.instroductionTv.setText(dataBean.getInstroduction());
//     获取图片的地址
        String pic = dataBean.getPicLink();
        holder.picIv.setVisibility(View.VISIBLE);
        imageLoader.displayImage(pic,holder.picIv,options);
        return convertView;
    }


    class ViewHolder{
        ImageView picIv;
        TextView nameTv,infoTv,scoreTv,ratedTv,instroductionTv;
        public ViewHolder(View view){
            picIv = view.findViewById(R.id.movie_pic);
            nameTv = view.findViewById(R.id.movie_name);
            infoTv = view.findViewById(R.id.movie_info);
            scoreTv = view.findViewById(R.id.movie_score);
            ratedTv = view.findViewById(R.id.movie_rated);
            instroductionTv = view.findViewById(R.id.movie_instroduction);
        }
    }
}
